package app.cloudstringers;

import java.util.Timer;
import java.util.TimerTask;

import utils.Utils;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import controller.RefreshStatusAsync;
import define.Constants;

@SuppressWarnings("deprecation")
public class StatusTab extends TabActivity {
	/**
	 * String section
	 */
	public static boolean FIRST_LAUNCH_TIME = true;
	public static boolean SECOND_LAUNCH_TIME = false;

	/**
	 * View section
	 */
	public static TabHost tabHost;

	/**
	 * The others section
	 */
	private static Handler mHandler = new Handler();
	public static Timer mTimerUpload = null;
	private static Timer mTimerConvert = null;

	// Timer task
	public static TimerTask mTtRefreshUpload = new TimerTask() {
		@Override
		public void run() {
			mHandler.post(new Runnable() {

				@Override
				public void run() {
					refreshTab(Upload.class);
				}
			});
		}
	};

	// Timer task
	public static TimerTask mTtRefreshConvert = new TimerTask() {
		@Override
		public void run() {
			mHandler.post(new Runnable() {

				@Override
				public void run() {
					refreshTab(Convert.class);
				}
			});
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Initial connection dialog
		Utils.initialConnectionDialog(this);

		// Initial variables
		tabHost = getTabHost();
		TabHost.TabSpec spec;
		Intent intent;

		// new intent to Upload class
		intent = new Intent().setClass(this, Upload.class);
		spec = tabHost.newTabSpec(getString(R.string.upload));
		spec.setIndicator(getString(R.string.upload),
				getResources().getDrawable(R.drawable.ic_tab_upload_normal))
				.setContent(intent);
		tabHost.addTab(spec);

		// new intent to Priority List class
		intent = new Intent().setClass(this, PriorityList.class);
		spec = tabHost.newTabSpec(getString(R.string.priority_list));
		spec.setIndicator(getString(R.string.priority_list),
				getResources().getDrawable(R.drawable.ic_tab_priority_list_normal))
				.setContent(intent);
		tabHost.addTab(spec);

		// new intent to Convert class
		intent = new Intent().setClass(this, Convert.class);
		spec = tabHost.newTabSpec(getString(R.string.convert));
		spec.setIndicator(getString(R.string.convert),
				getResources().getDrawable(R.drawable.ic_tab_convert_normal))
				.setContent(intent);
		tabHost.addTab(spec);

		// new intent to Completed class
		intent = new Intent().setClass(this, Completed.class);
		spec = tabHost.newTabSpec(getString(R.string.completed));
		spec.setIndicator(getString(R.string.completed),
				getResources().getDrawable(R.drawable.ic_tab_completed_normal))
				.setContent(intent);
		tabHost.addTab(spec);

		// Set current tab
		tabHost.setCurrentTab(0);

		// Temporary disable all tab widget
		Utils.disableTabWidgetInStatusTab(Constants.STATUS_TAB_SIZE);

		tabHost.setOnTabChangedListener(new OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {
				tabChanged(tabId);
			}
		});
	}

	@Override
	protected void onDestroy() {
		cancelTimer();

		super.onDestroy();
	}

	@Override
	protected void onPause() {
		cancelTimer();

		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();

		// Get tab current to check
		switch (tabHost.getCurrentTab()) {
		case Constants.TAB_UPLOAD:
			// Upload Tab
			if (mTimerUpload == null) {
				mTimerUpload = new Timer();

				if (mTtRefreshUpload == null) {
					mTtRefreshUpload = new TimerTask() {
						@Override
						public void run() {
							refreshTab(Upload.class);
						}
					};
				}

				mTimerUpload.schedule(mTtRefreshUpload, 0,
						define.Timer.TIMER_REFRESH_IN_STATUS_TAB);
			}
			break;
		case Constants.TAB_CONVERT:
			// Convert Tab
			if (mTimerConvert == null) {
				mTimerConvert = new Timer();
				mTimerConvert.schedule(mTtRefreshConvert, 0,
						define.Timer.TIMER_REFRESH_IN_STATUS_TAB);
			}
			break;
		default:
			// Priority List Tab and Completed Tab
			cancelTimer();

			break;
		}
	}

	public static void cancelTimer() {
		mTtRefreshUpload = null;
		mTtRefreshConvert = null;
		if (mTimerUpload != null) {
			mTimerUpload.cancel();
			mTimerUpload = null;
		}
		if (mTimerConvert != null) {
			mTimerConvert.cancel();
			mTimerConvert = null;
		}
	}

	public static void refreshTab(Class<?> object) {
		// If the connection dialog has shown, dismiss it before show new dialog
		if (!Constants.utils.isNetworkOnline()) {
			Utils.hideConnectionDialog();
		} else {
			if (object.getSimpleName().equals(Upload.class.getSimpleName())) {
				// Check boolean not to allow refresh two times in the same time
				if (Constants.REFRESH_IN_UPLOAD) {
					// If true
					// Refresh list item after 7 seconds to see new items
					new RefreshStatusAsync(Upload.mContextUpload).execute(
							Home.ID, Constants.STATE_UPLOAD + "");
				}
			} else if (object.getSimpleName().equals(
					Convert.class.getSimpleName())) {
				// Refresh list item after 7 seconds to see new items
				new RefreshStatusAsync(Convert.mContextUpload).execute(Home.ID,
						Constants.STATE_CONVERT + "");
			}
		}
	}

	/**
	 * Basic methods
	 */
	public void tabChanged(String tabId) {
		if (tabId.equals(define.Class.CLASS_UPLOAD)) {
			// Call new upload thread
			if (mTimerUpload == null) {
				// Set update timer, update list view after 7 seconds
				mTimerUpload = new Timer();
				if (mTtRefreshUpload == null) {
					mTtRefreshUpload = new TimerTask() {
						@Override
						public void run() {
							refreshTab(Upload.class);
						}
					};
				}

				mTimerUpload.schedule(mTtRefreshUpload, 0,
						define.Timer.TIMER_REFRESH_IN_STATUS_TAB);
			}

			mTtRefreshConvert = null;
			if (mTimerConvert != null) {
				mTimerConvert.cancel();
				mTimerConvert = null;
			}
		} else if (tabId.equals(define.Class.CLASS_CONVERT)) {
			// Call new upload thread
			if (mTimerConvert == null) {
				// Set update timer, update list view after 7 seconds
				mTimerConvert = new Timer();
				if (mTtRefreshConvert == null) {
					mTtRefreshConvert = new TimerTask() {
						@Override
						public void run() {
							refreshTab(Convert.class);
						}
					};
				}

				mTimerConvert.schedule(mTtRefreshConvert, 0,
						define.Timer.TIMER_REFRESH_IN_STATUS_TAB);
			}

			mTtRefreshUpload = null;
			if (mTimerUpload != null) {
				mTimerUpload.cancel();
				mTimerUpload = null;
			}
		} else {
			// Priority List Tab & Completed Tab
			cancelTimer();
		}
	}
}
